<template>
  <div>
    <Title />
    <el-container>
      <el-main width="55%">
        <img src="@/assets/imgs/a.svg" alt="" style="width:60%;">
      </el-main>
      <el-aside width="45%">
        <el-card class="card" shadow="always">
          <el-form ref="registerFormRef" :model="registerInfo" :rules="registerFormRules">
            <h3>账号注册</h3>
            <el-form-item prop="userName">
              <el-input v-model="registerInfo.userName" placeholder="用户名" />
            </el-form-item>
            <el-form-item prop="passWord">
              <el-input v-model="registerInfo.passWord" placeholder="密码" show-password />
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="registerInfo.phone" placeholder="手机号" />
            </el-form-item>
            <el-form-item prop="validateCode">
              <el-col :span="12">
                <el-input v-model="registerInfo.validateCode" placeholder="验证码" maxlength="6"></el-input>
              </el-col>
              <el-col :span="11" :offset="1">
                <el-image :src="validateCodeSrc" style="width:100%;" @click="getValidateCode" ></el-image>
              </el-col>
            </el-form-item>
            <el-form-item>
              <el-button type="success" class="btn-login" @click="register">注册</el-button>
            </el-form-item>
            <p style="font-size:12px;">已有账号？<router-link to="/login" style="color:#3CB371;">立即登录</router-link></p>
          </el-form>
        </el-card>
      </el-aside>
    </el-container>
    <el-footer />
  </div>
</template>

<script>
import Title from '@/components/title.vue'
import '@/static/css/login.css'
import {register,getValidateCode} from '@/api/user'
import {checkPhone} from '@/utils/validate'

export default {
  name: 'Register',
  components: { Title },
  data() {
    return {
      registerInfo: {
        userName: '',
        phone: '',
        passWord: '',
        validateCode: ''
      },
      validateCodeSrc:"",
      registerFormRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        passWord: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '至少6个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkPhone, trigger: "blur" }
        ],
        validateCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    }
  },
  mounted(){
    this.getValidateCode()
  },
  methods: {
    async getValidateCode() {
      const res = await getValidateCode()
      this.validateCodeSrc=res.data;
    },
    register() {
      this.$refs.registerFormRef.validate(async valid => {
        if (!valid) return
        const res = await register(this.registerInfo)
        console.log(res)
        if (res.code === 101) {
          this.$message.success('注册成功')
          this.$router.push('/login')
        }
      })
    }
  }
}
</script>

<style  scoped>
.card{
    width:60%;
    padding:10px 30px;
}
</style>
